package main.java;

import java.util.Stack;

/**
 * @program: pratice
 * @author: 关键我是大怪
 * @create: 2021-07-25 20:01
 */
public class Test04_从尾到头打印链表 {

    /**
     * 题目:
     * 输入一个链表的头节点，
     * 从尾到头反过来返回每个节点的值（用数组返回）。
     * <p>
     * 思路：
     * 遍历链表，将将数字放进栈中
     * 栈先进后出，放入数组
     */


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public int[] test(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.add(head);
            head = head.next;
        }
        int x = stack.size();
        int[] a = new int[stack.size()];
        for (int i = 0; i < x; i++) {
            a[i] = stack.pop().val;
        }

        return a;
    }
}